<?php
namespace App\Http\Controllers\Wx;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\ApiRequest;
use App\Services\Wechat;
use App\Repos\OrderGroupRepo;
use App\Repos\OrderRepo;
use DB,Log;
use EasyWeChat;
use function EasyWeChat\Kernel\Support\generate_sign;

class OrderController extends Controller
{
    
    public function index(ApiRequest $request)
    {
        $rs['status']       =  true;        
        $where['user_id']   = $request->user_id; 
        if($request->status) {
            $where['status'] = $request->status;
        }       
        $rs['data'] = OrderRepo::pages($where,5);
        return response()->json($rs);

    }

    public function add(ApiRequest $request)
    {
        $rs['status'] = true;    
        $data = $request->pure();    
        $data['openid']  = $request->user->small_openid;
        $data['user_id'] = $request->user_id;
        $flag = OrderRepo::create($data);
        if($flag['status'] ==  true) {
            $rs['data'] = $flag['data'];
        }
    
        return response()->json($rs);
    }

    /**
     * 团购订单
     */
    public function addGroup(ApiRequest $request)
    {
        $rs['status'] = true;    
        $data = $request->pure();    
        $data['openid']  = $request->user->small_openid;
        $data['user_id'] = $request->user_id;
        //dd($data);
        $flag = OrderRepo::createGroup($data);
        if($flag['status'] ==  true) {
            $rs['data'] = $flag['data'];
        }

    
        return response()->json($flag);
    }

    

    public function info(ApiRequest $request)
    {
        $rs['status'] = false;
        $where['id']      = $request->order_id;
        $where['user_id'] = $request->user_id;
        $order = OrderRepo::find($where);
        if($order) {
            $rs['status'] = true;
            $rs['data']   = $order;
           
            return response()->json($rs);
        }

        $rs['msg'] = '订单错误';
        return response()->json($rs);
    }

    


    // 用户确认收货
    public function confirmReceived(ApiRequest $request)
    {
        // 用户点击确认的时候会穿过一个form_id
        $data = $request->pure();        
        $rs = OrderRepo::confirmReceived($data);      
        return response()->json($rs);
    }

    public function orderNum(ApiRequest $request)
    {
        $where['user_id'] = $request->user_id;

        $where['status'] = [1];
        $data['order_1'] = OrderRepo::orderCount($where);

        $where['status'] = [50];
        $data['order_2'] = OrderRepo::orderCount($where);

        $where['status'] = [2,60];
        $data['order_3'] = OrderRepo::orderCount($where);

        $where['status'] = [4];
        $data['order_4'] = OrderRepo::orderCount($where);

        $rs['status'] = true;
        $rs['data'] = $data;
        return response()->json($rs);

    }

    public function cancle(ApiRequest $request)
    {
        $data = $request->pure();
        $rs = OrderRepo::cancle($data);
       
        return response()->json($rs);

    }

    /**
     * 团购详情
     */
    public function groupInfo(Request $request)
    {
        $group_code = $request->group_code;
        $data = OrderGroupRepo::info($group_code);
        $rs['data'] = $data;
        $rs['status'] = true;
        return response()->json($rs);
    }

    public function pay(ApiRequest $request)
    {
        $data = $request->pure();
        $rs = OrderRepo::pay($data);
       
        return response()->json($rs);

    }


    

   

    
}
